package com.gin.kotlin.no_gay_alliance.adapter

import android.content.Context
import android.widget.CompoundButton
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.GlideModule.Companion.CROSS_FADE
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.TextViewTarget
import com.gin.kotlin.no_gay_alliance.databinding.ItemSearchAreaBinding
import com.gin.kotlin.no_gay_alliance.model.SearchAreaModel

/**
 * 搜索区域适配器
 */
class SearchAreaAdapter(context: Context, handler: MyHandler, data: MutableList<SearchAreaModel> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemSearchAreaBinding, SearchAreaModel>(context, handler, false, data), CompoundButton.OnCheckedChangeListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemSearchAreaBinding>, position: Int, b: ItemSearchAreaBinding, dataModel: SearchAreaModel) {
        if (data.size == 1) {
            b.root.isEnabled = false
            b.root.isChecked = true
            dataModel.checked = true
        }else{
            b.root.isEnabled = true
        }

        b.root.text = dataModel.name
        b.root.isChecked = dataModel.checked
        b.root.setOnCheckedChangeListener(this)
        b.root.tag = dataModel

        // 图标大小（dp）
        val size = (32 * context.resources.displayMetrics.density).toInt()

        // 加载版面头像
        Glide.with(context)
            .load(dataModel.icon)
            .error(R.drawable.ic_forum_avatar)
            .placeholder(R.drawable.ic_forum_avatar)
            .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
            .into(TextViewTarget(b.root, 0, size, size))
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        val dataModel = buttonView?.tag as? SearchAreaModel ?: return
        dataModel.checked = isChecked
    }
}